﻿using MiniFox.Components;
using MiniFox.Configurations;
using Wechat.MP.Configurations;
using Wechat.MP.Models;

namespace Wechat.MP
{
    /// <summary>
    /// 
    /// </summary>
    [AutoRegister, SpareComponent(typeof(DefaultWechatMpConfigProvider))]
    public abstract class WechatMpConfigProvider
    {
        /// <summary>
        /// 
        /// </summary>
        protected WechatMpConfigProvider()
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        abstract public IWechatMpConfiguration GetSecureConfig();
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        abstract public IWechatClient GetClientSetting(string name);

    }

    internal class DefaultWechatMpConfigProvider : WechatMpConfigProvider
    {
        IWechatMpConfiguration _config;
        public override IWechatMpConfiguration GetSecureConfig()
        {
            if (_config == null)
            {
                var config = ConfigurationFactory.GetConfig<WechatMpSecurityConfiguration>();
                _config = new WechatMpSecure
                {
                    Default = config.Default,
                    WechatClients = config.WechatClients
                };
            }
            return _config;
        }
        public override IWechatClient GetClientSetting(string name)
        {
            var config = this.GetSecureConfig();
            name = (string.IsNullOrWhiteSpace(name)) ? config.Default : name;
            return config.WechatClients[name];
        }

    }
}
